package com.lagou.plugin;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.util.Properties;

/**
 * 针对 executor 组件的query进行增强,
 *
 * @author srh
 * @Intercepts 拦截器的意思, @Signature 用于指定具体的拦截信息
 * @date 2020/02/27
 **/
@Intercepts({
        @Signature(
                type = Executor.class,
                method = "query",
                args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
        )
})
public class ExamplePlugin implements Interceptor {

    /**
     * 每次执行的都会执行拦截
     *
     * @param invocation
     * @return
     * @throws Throwable
     */
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println("对此方法进行增强...");
        return invocation.proceed();
    }

    /**
     * 将target生成代理对象并加入到拦截器中
     *
     * @param target
     * @return
     */
    @Override
    public Object plugin(Object target) {
        System.out.println("获取的原代理对象" + target);
        return Plugin.wrap(target, this);
    }

    /**
     * 获取配置文件的属性，插件初始化的时候会调用一次
     *
     * @param properties
     */
    @Override
    public void setProperties(Properties properties) {
        System.out.println("获取插件的配置信息" + properties);
    }
}
